package com.yuesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuesystem.common.CustomException;
import com.yuesystem.dto.responsebody.LogResponseBody;
import com.yuesystem.entity.ChangeItem1;
import com.yuesystem.entity.User;
import com.yuesystem.mapper.ChangeItem1Mapper;
import com.yuesystem.service.AcademyItemService;
import com.yuesystem.service.AcademyService;
import com.yuesystem.service.ChangeItem1Service;
import com.yuesystem.service.UserService;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;




/**
 * 改变书院条目接口实现类.
 */
@Service
@Slf4j
public class ChangeItem1ServiceImpl extends ServiceImpl<ChangeItem1Mapper, ChangeItem1>
    implements ChangeItem1Service {
  @Autowired
  private AcademyItemService academyItemService;
  
  @Autowired
  private OfficeItemServiceImpl officeItemService;
  
  @Autowired
  private AcademyService academyService;
  
  @Autowired
  private UserService userService;
  
  /**
   * 根据时间返回queryWrapper.
   */
  @Override
  public LambdaQueryWrapper<ChangeItem1> help1(String startTime, String endTime) {
    LambdaQueryWrapper<ChangeItem1> queryWrapper = new LambdaQueryWrapper<>();
    
    if ((!startTime.isEmpty()) && (!endTime.isEmpty())) {
      startTime = startTime + " 00:00:00";
      endTime = endTime + " 23:59:59";
      DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
      LocalDateTime start = LocalDateTime.parse(startTime, df);
      LocalDateTime end = LocalDateTime.parse(endTime, df);
      queryWrapper.ge(ChangeItem1::getCreateTime, start)
          .le(ChangeItem1::getCreateTime, end);
    } else if (!startTime.isEmpty()) {
      startTime = startTime + " 00:00:00";
      DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
      LocalDateTime start = LocalDateTime.parse(startTime, df);
      queryWrapper.ge(ChangeItem1::getCreateTime, start);
      
    } else if (!endTime.isEmpty()) {
      endTime = endTime + " 23:59:59";
      DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
      LocalDateTime end = LocalDateTime.parse(endTime, df);
      queryWrapper.le(ChangeItem1::getCreateTime, end);
    }
    return queryWrapper;
  }
  
  /**
   * 分页查询个人日志信息.
   */
  @Override
  public Page<LogResponseBody> getPage(Long userId, int page, int pageSize, String startTime,
                                       String endTime, String operation, String academyName) {
    LambdaQueryWrapper<ChangeItem1> queryWrapper = this.help1(startTime, endTime);
    queryWrapper.eq(userId != null, ChangeItem1::getUserId, userId);
    log.info("操作是:{}", operation);
    if (!operation.isEmpty()) {
      log.info("操作不为空");
      if (operation.equals("增加") || operation.equals("删除") || operation.equals("修改")) {
        queryWrapper.eq(ChangeItem1::getOperate, operation);
      } else {
        // 输入的操作不存在
        throw new CustomException("不存在该操作，请重新输入");
      }
    }
    Page<ChangeItem1> pageInfo = new Page<>(page, pageSize);
    Page<LogResponseBody> dtoPage = new Page<>(page, pageSize);
    queryWrapper.orderByAsc(ChangeItem1::getCreateTime);
    this.page(pageInfo, queryWrapper);
    BeanUtils.copyProperties(pageInfo, dtoPage, "records");
    List<ChangeItem1> records = pageInfo.getRecords();
    List<LogResponseBody> dtoList = records.stream().map((item) -> {
      LogResponseBody logResponseBody = new LogResponseBody();
      BeanUtils.copyProperties(item, logResponseBody);
      // 获得用户id
      Long userId1 = item.getUserId();
      User one = userService.getById(userId1);
      if (one != null) {
        logResponseBody.setUserName(one.getUserName());
        logResponseBody.setPhone(one.getPhone());
      }
      return logResponseBody;
    }).collect(Collectors.toList());
    dtoPage.setRecords(dtoList);
    return dtoPage;
  }
  
  /**
   * 分页查询所有日志信息.
   *
   */
  @Override
  public Page<LogResponseBody> getAllPage(String name, int page, int pageSize, String startTime,
                                          String endTime, String operation, String academyName) {
    Page<LogResponseBody> dtoPage = new Page<>(page, pageSize);
    LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
    wrapper.like(User::getUserName, name);
    List<User> users = userService.list(wrapper);
    List<Long> ids = users.stream().map((item) -> {
      return item.getId();
    }).collect(Collectors.toList());
    LambdaQueryWrapper<ChangeItem1> queryWrapper = this.help1(startTime, endTime);
    queryWrapper.in(ids.size() != 0, ChangeItem1::getUserId, ids);
    if (ids.size() == 0) {
      return dtoPage;
    }
    if (!operation.isEmpty()) {
      log.info("操作不为空");
      if (operation.equals("增加") || operation.equals("删除") || operation.equals("修改")) {
        queryWrapper.eq(ChangeItem1::getOperate, operation);
      } else {
        throw new CustomException("不存在该操作，请重新输入");
      }
    }
    Page<ChangeItem1> pageInfo = new Page<>(page, pageSize);
    this.page(pageInfo, queryWrapper);
    BeanUtils.copyProperties(pageInfo, dtoPage, "records");
    List<ChangeItem1> records = pageInfo.getRecords();
    List<LogResponseBody> dtoList = records.stream().map((item) -> {
      LogResponseBody logResponseBody = new LogResponseBody();
      BeanUtils.copyProperties(item, logResponseBody);
      Long userId1 = item.getUserId();
      User one = userService.getById(userId1);
      if (one != null) {
        logResponseBody.setUserName(one.getUserName());
        logResponseBody.setPhone(one.getPhone());
      }
      return logResponseBody;
    }).collect(Collectors.toList());
    dtoPage.setRecords(dtoList);
    return dtoPage;
  }
}
